<template>
  <div>
    <!-- 导航面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 列表 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input v-model="userForm.query" placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="requestUsers">查询用户</el-button>
        </el-col>
      </el-row>

      <el-table :data="users" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <el-table-column label="电话" prop="mobile"></el-table-column>
        <el-table-column label="角色" prop="role_name"></el-table-column>

        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.mg_state"></el-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeUserById(scope.row.id)"></el-button>
            <!-- 分配角色按钮 -->
            <el-tooltip effect="dark" content="分配角色" placement="top" :enterable="false">
              <el-button type="warning" icon="el-icon-setting" size="mini" @click="setRole(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页控件 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="userForm.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="userForm.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 修改用户的对话框 -->
    <el-dialog title="修改用户" :visible.sync="dialogEditFormVisible" width="50%">
      <el-form :model="editForm" :rules="editFormRules" ref="ruleEditForm" label-width="70px">
        <el-form-item label="用户名">
          <el-input v-model="editForm.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email" required>
          <el-input v-model="editForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile" required>
          <el-input v-model="editForm.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserInfo">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUsers, getUserByID, editUser } from "@/api/user";
export default {
  data() {
    var checkMobile = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;

      if (regMobile.test(value)) {
        return cb();
      }

      cb(new Error("请输入合法的手机号"));
    };
    return {
      userForm: {
        query: "",
        pagenum: 1,
        pagesize: 1
      },
      users: [],
      total: 0,
      dialogEditFormVisible: false,
      editForm: {},
      editFormRules: {
        email: [
          {
            required: true,
            message: "请输入用户邮箱",
            trigger: "blur"
          },
          {
            type: "email",
            message: "请输入正确的邮箱",
            trigger: "change"
          }
        ],
        mobile: [
          {
            validator: checkMobile,
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.requestUsers();
  },
  components: {},

  methods: {
    requestUsers() {
      getUsers(this.userForm).then(res => {
        if (res.meta.status !== 200) return this.$message.error("获取用户列表有误");
        console.log(res.data);
        this.total = res.data.total;
        this.users = res.data.users;
      });
    },
    handleSizeChange(val) {
      this.userForm.pagesize = val;
      this.requestUsers();
    },
    handleCurrentChange(val) {
      this.userForm.pagenum = val;
      this.requestUsers();
    },
    showEditDialog(userID) {
      getUserByID(userID).then(res => {
        if (res.meta.status !== 200) return this.$$message.error("获取用户详情失败");
        this.editForm = res.data;
        this.dialogEditFormVisible = true;
      });
    },
    editUserInfo() {
      this.$refs["ruleEditForm"].validate(valid => {
        if (valid) {
          editUser(this.editForm.id, this.editForm.email, this.editForm.mobile).then(res => {
            if (res.meta.status !== 200) return this.$message.error("编辑用户失败");
            this.editForm = {};
            this.dialogEditFormVisible = false;
            this.requestUsers();
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style scoped></style>
